package com.tfjybj.itoo.teach.provider.facade;

import org.apache.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.teach.FacadeApi.TrainingProgramsApi;
import com.tfjybj.itoo.teach.entity.TrainingProgramsEntity;
import com.tfjybj.itoo.teach.model.TrainingProgramsModel;
import com.tfjybj.itoo.teach.provider.service.TrainingProgramsService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * Created by 郝雨烁 on 2019/5/21.
 */
@Service
@org.springframework.stereotype.Service
public class TrainingProgramsFacade implements TrainingProgramsApi {

    @Autowired
    TrainingProgramsService trainingProgramsService;

    @Override
    public boolean create(TrainingProgramsModel model){
        if (StringUtils.isEmpty(model.getSemesterId())) {
            return  false;
        }
        if (StringUtils.isEmpty(model.getAcademyId())) {
            return  false;
        }
        if (StringUtils.isEmpty(model.getProfessionId())) {
            return  false;
        }
        if (StringUtils.isEmpty(model.getGrade())) {
            return  false;
        }
        if (StringUtils.isEmpty(model.getCourseId())) {
            return  false;
        }
        if (StringUtils.isEmpty(model.getCourseTypeId())) {
            return  false;
        }
        if (StringUtils.isEmpty(model.getCourseCategoryId())) {
            return  false;
        }
        if (StringUtils.isEmpty(model.getExaminationFormId())) {
            return  false;
        }
        TrainingProgramsEntity trainingProgramsEntity = new TrainingProgramsEntity();
        BeanUtils.copyProperties(model, trainingProgramsEntity);
        trainingProgramsEntity.setId(IdWorker.getIdStr());
        trainingProgramsEntity.setIsArrangement(0);
        String idIsExist = trainingProgramsService.findIdIsExist(trainingProgramsEntity.getSemesterId(), trainingProgramsEntity.getProfessionId(), trainingProgramsEntity.getCourseId(), trainingProgramsEntity.getGrade());
        if (!StringUtils.isEmpty(idIsExist)) {
           return  false;
        }
        return  trainingProgramsService.save(trainingProgramsEntity);
    }

    @Override
    public boolean delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        return  trainingProgramsService.removeById(id);
    }


    @Override
    public boolean deleteByIds(List<String> ids) {
        return  trainingProgramsService.removeByIds(ids);
    }


    @Override
    public boolean modify( TrainingProgramsModel model) {
        if (StringUtils.isEmpty(model.getCourseId())) {
           return false;
        }
        if (StringUtils.isEmpty(model.getProfessionId())) {
            return false;
        }
        if (StringUtils.isEmpty(model.getSemesterId())) {
            return false;
        }
        TrainingProgramsEntity trainingProgramsEntity = new TrainingProgramsEntity();
        BeanUtils.copyProperties(model, trainingProgramsEntity);
        String idIsExist = trainingProgramsService.findIdIsExist(trainingProgramsEntity.getSemesterId(), trainingProgramsEntity.getProfessionId(), trainingProgramsEntity.getCourseId(), trainingProgramsEntity.getGrade());
        if (!StringUtils.isEmpty(idIsExist)) {
            return false;
        }
        return trainingProgramsService.updateById(trainingProgramsEntity);

    }

    @Override
    public PageInfo<TrainingProgramsEntity> queryPageAll(Integer pageNo, Integer pageSize) {
        return  trainingProgramsService.queryPageAll(pageNo, pageSize);
    }

    @Override
    public TrainingProgramsEntity findById(String id) {
        TrainingProgramsEntity trainingProgramsEntity = trainingProgramsService.getById(id);
        return trainingProgramsEntity;
    }


    @Override
    public PageInfo<TrainingProgramsEntity> queryByCourseId(String courseId, Integer pageNo, Integer pageSize) {
        PageInfo<TrainingProgramsEntity> trainingProgramsList = trainingProgramsService.queryByCourseId(courseId, pageNo, pageSize);
        return trainingProgramsList;
    }
    @Override
   public List<TrainingProgramsEntity> findCourseId(){
          List<TrainingProgramsEntity> trainingProgramsEntityList = trainingProgramsService.findCourseId();
          return trainingProgramsEntityList;
   }

   @Override
   public List<TrainingProgramsModel> findByProfessionId(String professionId){
       List<TrainingProgramsModel> trainingProgramsModelList = trainingProgramsService.findByProfessionId(professionId);
       return  trainingProgramsModelList;
   }

   @Override
   public List<TrainingProgramsModel> findCourseByProfessionId(String professionId){
       List<TrainingProgramsModel> trainingProgramsModelList = trainingProgramsService.findCourseByProfessionId(professionId);
       return trainingProgramsModelList;
   }
}
